<?php
/**
 *	description:ZMAXWECHAT media类实现文件
 *  author：min.zhang
 *  Email:zhang19min88@163.com
 *	Url:http://www.zmax99.com
 *  copyright:南宁市程序人软件科技有限责任公司保留所有权利
 *  date:2016-04-28
 *  @license GNU General Public License version 3, or later
 */
 
defined('_JEXEC') or die('Restricted access');
jimport('joomla.http.http');
jimport('joomla.http.transport.curl');

class zmaxwechatMedia
{
	//获得素材列表的URL
	protected $mediaListUrl = 'https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=';
	protected $getMediaUrl = 'https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=';
	protected $updateMediaUrl='https://api.weixin.qq.com/cgi-bin/material/update_news?access_token=';
	protected $sendNewsUrl='https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=';
	protected $addNewsUrl = 'https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=';
	protected $uploadImageUrl='https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=';
	protected $addImageToServerUrl='https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=';
	protected $access_token;
	
	public function __construct($accessToken)
	{
		$this->access_token = $accessToken;
		$this->mediaListUrl=$this->mediaListUrl.$this->access_token;
		$this->updateMediaUrl = $this->updateMediaUrl.$this->access_token;
		$this->sendNewsUrl = $this->sendNewsUrl.$this->access_token;
		$this->uploadImageUrl = $this->uploadImageUrl.$this->access_token."&type=image";
		$this->addImageToServerUrl = $this->addImageToServerUrl.$this->access_token."&type=image";
		$this->addNewsUrl = $this->addNewsUrl.$this->access_token;
	}
	
	//获得素材列表
	public function getMediaList($type ,$offset ,$count)
	{		
		$options = new JRegistry();		
		$http = new JHttpTransportCurl($options);
		$params = array("type"=>$type ,'offset'=>$offset ,'count'=>$count);
		$params = json_encode($params,JSON_UNESCAPED_UNICODE);
		$url = new JUri($this->mediaListUrl);
		$response = $http->request("POST" ,$url ,$params);
		$medias = json_decode($response->body);
		return $medias;
	}
	
	//更新素材列表
	public function updateMedia($mediaId ,$index,$new)
	{
		$options = new JRegistry();		
		$http = new JHttpTransportCurl($options);
		$params = array("media_id"=>$mediaId ,'index'=>$index ,'articles'=>$new);
		$params = json_encode($params,JSON_UNESCAPED_UNICODE);
		$url = new JUri($this->updateMediaUrl);
		$response = $http->request("POST" ,$url ,$params);
		$result = json_decode($response->body);
		return $result;
	}
	
	//发送消息
	public function sendNews($filter ,$news ,$msgtype="mpnews")
	{
		$options = new JRegistry();		
		$http = new JHttpTransportCurl($options);
		$params = array("filter"=>$filter ,'mpnews'=>$news ,'msgtype'=>$msgtype);
		$params = json_encode($params,JSON_UNESCAPED_UNICODE);
		$url = new JUri($this->sendNewsUrl);
		$response = $http->request("POST" ,$url ,$params);
		$result = json_decode($response->body);
		return $result;
	}
	
	//添加图文消息
	public function addNews($news)
	{
		$options = new JRegistry();		
		$http = new JHttpTransportCurl($options);
		$params = array("articles"=>$news );
		$params = json_encode($params,JSON_UNESCAPED_UNICODE);
		$url = new JUri($this->addNewsUrl);
		
		$response = $http->request("POST" ,$url ,$params);
		$result = json_decode($response->body);
		
		return $result;
		
	}
	
	//上传图片
	public function uploadImage($file)
	{
		$params['media']  = '@'.$file;  
        $result=$this->http_post_media($this->uploadImageUrl, $params);
		$url = json_decode($result);
		return  $url->url;
	}
	
	//向服务器增加永久素材
	public function addImageToServer($file)
	{
		$params['media']  = '@'.$file;  
        $result=$this->http_post_media($this->addImageToServerUrl, $params);
		return $result;
	}

	public function  http_post_media($url,$strPOST)  
	{
		$oCurl = curl_init ();  
        if (stripos ( $url, "https://" ) !== FALSE)
		{  
            curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYPEER, FALSE );  
            curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYHOST, false );  
        }  
  
        curl_setopt ( $oCurl, CURLOPT_URL, $url );  
        curl_setopt ( $oCurl, CURLOPT_RETURNTRANSFER, 1 );  
        curl_setopt ( $oCurl, CURLOPT_POST, true );  
        curl_setopt ( $oCurl, CURLOPT_POSTFIELDS, $strPOST );  
        $sContent = curl_exec ( $oCurl );  
        $aStatus = curl_getinfo ( $oCurl );  
        curl_close ( $oCurl );  
        if (intval ( $aStatus ["http_code"] ) == 200) 
		{  
            return $sContent;  
        } 
		else 
		{  
            return false;  
        }  
	}
	
	

}


